package com.qf.qfleadnewscommons.sms;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
//启用配置类
@EnableConfigurationProperties(SMSProperteis.class)
//当引入这个依赖时，必须配置一个sms.enable属性，值为true，才会注入这些对象。否则不注入
@ConditionalOnProperty(name = "sms.enable",havingValue = "true",matchIfMissing = false)
public class SMSAutoConfiguration {

    @Autowired
    private SMSProperteis properteis;

    @Bean
    public com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(properteis.getAccessKeyId())
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(properteis.getAccessKeySecret());
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    @Bean
    public SmsOperator smsOperator(com.aliyun.dysmsapi20170525.Client client){
        return new SmsOperator(client,properteis);
    }

}
